//
//  ZYEmoticonPageViewCell.swift
//  WBBeta1
//
//  Created by wustzhy on 16/8/18.
//  Copyright © 2016年 Yestin. All rights reserved.
//

import UIKit

class ZYEmoticonPageViewCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var emoticons:[ZYEmoticonModel]? {
        didSet{
            print(emoticons?.count)
            //108,20余下的复用cell问题
            for btn in buttonArray {
                btn.hidden = true
            }
            //
            for (i,emoticonModel) in emoticons!.enumerate() {
                let btn = buttonArray[i]
                //
                btn.hidden = false
                if emoticonModel.isEmoji {
                    let code = (emoticonModel.code ?? "" as NSString).emoji()
                    btn.setTitle(code, forState: UIControlState.Normal)
                    //防重用
                    btn.setImage(nil, forState: UIControlState.Normal)
                }else{
//                    let png = emoticonModel.png ?? ""//需要拼接
////                    let path = emoticonModel.path //这样不行！
//                    let path = emoticonModel.path ?? ""//这样才行   why？
//                    
//                    //从Bundle中取图的方式
//                    let img = UIImage(named: "\(path)\(png)", inBundle: ZYEmoticonTools.sharedTools.emoticonsBundle, compatibleWithTraitCollection: nil)
                    //  default/d_aini@2x.png   //拼接
//                    /Users/wustzhy/Desktop/oschinaProj/framework01/WBBeta1/WBBeta1/Classes/Compose/view/Emoticon/Emoticons.bundle/Contents/Resources/default/d_aini@2x.png
                    let img = UIImage.emoticonsImage(emoticonModel)
                    btn.setImage(img, forState: UIControlState.Normal)
                    //防重用
                     btn.setTitle(nil, forState: UIControlState.Normal)
                }
                
                
            }
            
        }
    }
    
    func setupUI() {
     backgroundColor = RandomColor()
        
        addChildButtons()
        //addSubview(msgLabel)//❌
        contentView.addSubview(msgLabel)
        
        msgLabel.snp_makeConstraints { (make) -> Void in
        //make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
            make.center.equalTo(contentView)
        }
    }
    
    //设置emoji图片button的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        
//        let w = SCREENW/ZYEmoticonsMaxCol
        let w = self.frame.width/CGFloat(ZYEmoticonsMaxCol)
        let h = self.frame.height/CGFloat(ZYEmoticonsMaxRow)
        for (index,button) in buttonArray.enumerate() {
            let col = CGFloat(index%ZYEmoticonsMaxCol)
            let row = CGFloat(index/ZYEmoticonsMaxCol)
            button.frame = CGRect(x: col*w, y: row*h, width: w, height: h)
        }
    }
//MARK: 创建button-- 一维数组 20
    var buttonArray:[UIButton] = [UIButton]()
    func addChildButtons(){
        for i in 0..<ZYEmoticonsMaxCount {
            let btn = UIButton()
            btn.titleLabel?.font = UIFont.systemFontOfSize(30)
            btn.backgroundColor = RandomColor()
            contentView.addSubview(btn)
            buttonArray.append(btn)
        }
    }
    
    
    
    var index:NSIndexPath?{
        didSet{
            msgLabel.text = "第\(index?.section ?? 0)组\n第\(index?.item ?? 0)个"
        }
        
    }
    private lazy var msgLabel:UILabel = UILabel(textColor: UIColor.redColor(), textSize: 35, maxWidth: SCREENW)
}
